<?php
namespace app\api\controller\v1;
class Goods extends Base{

    function lists(){
        $d = $this->post();
        $order = 0;
        $map = [];
        $page = 1;
        $limit = 20;
        if(!empty($d["order"]))$order=$d["order"];
        if(!empty($d["limit"]))$limit=$d["limit"];
        if (!empty($d["cat_id"])){
            $ids = m("GoodsCat", "c")->getAllchildCat($d["cat_id"]);
            if($ids){
                $str = "";
                foreach($ids as $v){
                    $str .= "?,";
                }
                $str = rtrim($str,",");
                $map[] = ["and", "cat_id in (".$str.")", $ids];
            }else{
                $map[] = ["and", "cat_id", "=", $d["cat_id"]];
            }
        }
        if(!empty($d["keyword"]))$map[] =["and","title","like",'%' . $d["keyword"] . '%'];
        if(!empty($d['page']))$page = $d["page"];
        $goods_list = m("goods","c")->lists($map,$order, $page,$limit);
        $list = [
            "goods_list"=>$goods_list[0],
            "page_count"=>$goods_list[1],
            "page" => $page
        ];
        suc($list);
    }

    //商品详情
    function detail(){
        $id = $this->post('id');
        if($id){
            $rs = $this->db->query('select id from goods_price where goods_id=? order by id asc',[$id],1);
        }
        $gid = $this->post('gid');
        if(empty($gid)){
            $gid = $rs['id'];
        }
        $info = m("goods", "c")->detail($gid);
        $info['spec'] = htmlspecialchars_decode($info['spec']);
        $info['content'] = htmlspecialchars_decode($info['content']);
        //同分类下人气商品
        $map = [
            ["and", "id", "!=", $info["goods_id"]],
            ["and", "cat_id", "=", $info["cat_id"]]
        ];
        $cat_goods = m("goods", "c")->lists($map, 3, 1, 2);
        //人气累加
        $d = [
            "views" => $info['p_views'] + 1,
            "id" => $id
        ];
        $this->db->update("goods_price", $d);
        $info['spec'] = htmlspecialchars_decode($info['spec']);
        if($info['pics']){
          $info['pics'] = explode(",",$info["pics"]);
        }
        //属性
        $attr = [];
        $spec = json_decode($info['spec'],true);
        $ptitle = explode("_",$info['p_title']);
        $temp = [];
        $i = 0;
        foreach($spec as $k => $v){
            $arr = [
                'name'=>$k,
                'lists'=>[],
            ];
            foreach($v as $key => $val){
                $arr_1 = [];
                $arr_1['title'] = $val;
                $arr_1['checked'] = $val==$ptitle[$i] ? 1:0;
                $arr['lists'][] = $arr_1;
            }
            $temp[] = $arr;
            $i++;
        }
        $iscollect = 0;
        if($this->userinfo){
            $collectinfo = $this->db->query('select * from goods_collect where goods_id=? and user_id=?',[$info['goods_id'],$this->userinfo["id"]]);
            if($collectinfo){
                $iscollect = 1;
            }
        }
        $info['iscollect'] = $iscollect;
        $info['h5_url'] = url('h5/goodsdetail',['id'=>$info['id']]);
        $info['spec'] = $temp;
        $d = [
            "goods_info" => $info,
            "cat_goods" => $cat_goods[0],
        ];
        suc($d);
    }

    public function findGoods(){
        $d = $this->post;
        $info = $this->db->query('select * from goods_price where title = ? and goods_id =?',[$d['title'],$d['goods_id']],1);
        $d = [
            "goods_info" => $info,
        ];
        suc($d);
    }

}
?>
